<?php
/**
 * Created by PhpStorm.
 * User: 伟鑫
 * Date: 2019/3/11
 * Time: 10:25
 */

namespace app\admin\controller;


use app\admin\model\AreaModel;
use app\admin\model\ShifuModel;
use think\Db;
use think\Request;

class Shifu extends Base{

    public function index(Request $request){
        $where = [];
        $param = $request->param();
        if (isset($param['real_name']) && !empty($param['real_name'])) $where['real_name'] = ['like','%'.$param['real_name'].'%'];
        if (isset($param['phone']) && !empty($param['phone'])) $where['phone'] = $param['phone'];
        if (isset($param['prohibit']) && !empty($param['prohibit'])) $where['prohibit'] = $param['prohibit'];
        $where['type'] = 1;
        $data = Db::name('member')->where($where)->paginate(10);
        $this->assign('list',$data);
        return $this->fetch();
    }

    public function info(Request $request){
        $info = ShifuModel::get($request->param('id'));
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function edit(Request $request){
        if ($request->isAjax()){
            return (new ShifuModel())->edit();
        }
    }


    public function shenhe(Request $request){
        if ($request->isAjax()){
            $user = ShifuModel::get($request->param('id'));
            if ($request->param('step') == 1){
                $user->step = 2;
            }elseif ($request->param('step') == 3){
                $user->step = 4;
            }
            if ($user->save()){
                return msg(1,url('index'),'操作成功');
            }else{
                return msg(-1,[],'操作失败');
            }
        }
    }

    /**
     * 禁止接单
     */
    public function prohibit(Request $request){
        if ($request->isAjax()){
            $shufu = ShifuModel::get($request->post('id'));
            $shufu->prohibit = ($request->post('prohibit') == 1)?2:1;
            if ($shufu->save()){
                return msg(1,url('index'),'操作成功');
            }else{
                return msg(-1,[],'操作失败');
            }
        }
    }

    /**
     * 修改服务项目
     * @param Request $request
     * @return array|mixed
     */
    public function jineng(Request $request){
        $id = $request->param('id');
        $shufu = ShifuModel::get($id);
        if ($request->isAjax()){
            $fenleiid = $request->param();
            $shufu->fenleiid = implode(',',$fenleiid['fenleiid']);
            if ($shufu->save()){
                return msg(1,url('index'),'操作成功');
            }else{
                return msg(-1,[],'操作失败');
            }
        }
        $fenlei = \app\admin\model\Goodstype::all();
        $old = explode(',',$shufu->getData('fenleiid'));
        $this->assign(compact('fenlei','id','old'));
        return $this->fetch();
    }
    /**
     * 修改服务区域
     * @param Request $request
     * @return array|mixed
     */
    public function quyu(Request $request){
        $id = $request->param('id');
        $shufu = ShifuModel::get($id);
        if ($request->isAjax()){
            $fenleiid = $request->param();
            $xian = explode(',',$shufu->getData('xian'));
            $xian_flip = array_flip($xian);
            if (isset($fenleiid['xian']) && !empty($fenleiid['xian'])){
                $arr = [];
                //获取原来服务区域的在当前区域的值
                foreach (session('city') as $v){
                    if (in_array($v,$xian) ){
                        $arr[] = $v;
                    }
                }
                //获取其他市下面的县
                $arr1 = array_diff($xian,$arr);
                $arr2 = array_intersect($arr,$fenleiid['xian']);
                $data = array_merge($arr2,$arr1);
            }else{
                foreach (session('city') as $v){
                    if (in_array($v,$xian) ){
                        unset($xian[$xian_flip[$v]]);
                    }
                }
                $data = $xian;
            }

            $shufu->xian = implode(',',$data);
            if ($shufu->save()){
                return msg(1,url('index'),'操作成功');
            }else{
                return msg(-1,[],'操作失败');
            }
        }
        $old = explode(',',$shufu->xian);
        $this->assign(compact('id','old'));
        return $this->fetch();
    }


    public function ajxgetcity($province,$id){
        $area = AreaModel::where('parent_id',$province)->select();
        $htm = '';
        $shufu = ShifuModel::get($id);
        $old = explode(',',$shufu->getData('xian'));
        $city = [];
        foreach ($area as $v){
            $city[] = $v['id'];
            $status = in_array($v['id'],$old)?'checked':'';
            $htm .= '<div class="radio i-checks col-sm-4">
                                    <label>
                                        <input type="checkbox" value="'.$v['id'].'" name="xian[]" '.$status.'> <i></i> '.$v['name'].'</label>
                                </div>';
        }
        session('city',$city);
        return $htm;
    }
}